import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class CourierSelectionWidget extends StatefulWidget {
  const CourierSelectionWidget({super.key});

  @override
  CourierSelectionWidgetState createState() => CourierSelectionWidgetState();
}

class CourierSelectionWidgetState extends State<CourierSelectionWidget> {
  // 当前选中的快递选项，默认值为列表的第一项
  String selectedCourier = '顺丰速运';

  // 弹出选择快递的底部弹窗并更新按钮文本
  Future<void> _showCourierSelectionDialog() async {
    List<String> options = [
      '顺丰速运',
      '中通快递',
      '圆通快递',
      '申通快递',
      '韵达快递',
      '百世快递',
      'EMS(中国邮政速递物流)',
      '京东物流',
      '菜鸟网络',
      '德邦物流',
      '天天快递',
      '极兔',
    ];

    // 初始选中的选项索引
    int selectedIndex = options.indexOf(selectedCourier);

    await showModalBottomSheet<String>(
      context: context,
      isScrollControlled: true,
      builder: (BuildContext context) {
        return Container(
          padding: const EdgeInsets.all(16),
          height: 300, // 控制弹窗高度
          child: Column(
            children: [
              const Text(
                "选择快递方式",
                style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
              ),
              const SizedBox(height: 10),
              Expanded(
                child: CupertinoPicker(
                  scrollController: FixedExtentScrollController(initialItem: selectedIndex),
                  itemExtent: 40.0, // 每个项的高度
                  onSelectedItemChanged: (index) {
                    setState(() {
                      selectedIndex = index;
                    });
                  },
                  children: List<Widget>.generate(options.length, (index) {
                    return Center(
                      child: Text(
                        options[index],
                        style: TextStyle(
                          fontSize: 16,
                          color: selectedIndex == index ? Colors.green : Colors.black,
                        ),
                      ),
                    );
                  }),
                ),
              ),
              const SizedBox(height: 20),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  SizedBox(
                    width: 100,
                    child: ElevatedButton(
                      style: ElevatedButton.styleFrom(
                        backgroundColor: Colors.grey[300],
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(8), // 调整这个值来改变圆角的大小
                        ),
                      ),
                      onPressed: () {
                        Navigator.of(context).pop(null); // 关闭弹窗，不返回选择项
                      },
                      child: const Text("取消", style: TextStyle(color: Colors.black)),
                    ),
                  ),
                  const SizedBox(width: 20),
                  SizedBox(
                    width: 100,
                    child: ElevatedButton(
                      style: ElevatedButton.styleFrom(
                        backgroundColor: Colors.green,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(8), // 调整这个值来改变圆角的大小
                        ),
                      ),
                      onPressed: () {
                        // 返回选中项文本
                        Navigator.of(context).pop(options[selectedIndex]);
                      },
                      child: const Text("确定", style: TextStyle(color: Colors.white)),
                    ),
                  ),
                ],
              ),
            ],
          ),
        );
      },
    ).then((result) {
      if (result != null) {
        setState(() {
          selectedCourier = result; // 更新按钮的文本
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return IconButton(
      onPressed: _showCourierSelectionDialog,
      icon: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Text(selectedCourier, style: const TextStyle(color: Colors.black)),
          const SizedBox(width: 8),
          const Icon(Icons.arrow_drop_down, color: Colors.black),
        ],
      ),
    );
  }
}
